<!DOCTYPE html>
<HTML>
<HEAD><meta name="viewport" content="width=device-width, initial-scale=1">
<Title>Javascript Clickable Charts</Title>
<link type='text/css' rel='Stylesheet' href="maxchartapi.css" />
</HEAD>
<body bgcolor="#FFFFFF" text="#000000" topmargin="0" leftmargin="0" rightmargin="0" marginwidth="0" marginheight="0">
<p class="heading0">ChartDirector 7.0 (Java Edition)</p>
<p class="heading1">Javascript Clickable Charts</p>
<hr class="separator">
<div class="content"><img src="images/jsclick_doc.png" width='612' height='480'><br><br>
This example demonstrates using ChartDirector image maps to invoke client side Javascript functions.<br><br>
In this example, a clickable area chart will be produced. Apart from responding to mouse clicks, detail information about the data points will be displayed in a dynamic HTML block when the mouse pointer moves over the chart. This is accomplished using "onmouseover" and "onmouseout" event handlers with client side Javascript.<br><br>
The code for this example is listed below.<br><br>
<b>[JSP Version]</b> jspdemo/jsclick.jsp
<div class='codeblock'><code class='pre'>&lt;%@page import="ChartDirector.*, java.util.*" %&gt;
&lt;%
//
// For demo purpose, we use hard coded data. In real life, the following data could come from a
// database.
//
double[] revenue = {4500, 5600, 6300, 8000, 12000, 14000, 16000, 20000, 24000, 28000};
double[] grossMargin = {62, 65, 63, 60, 55, 56, 57, 53, 52, 50};
double[] backLog = {563, 683, 788, 941, 1334, 1522, 1644, 1905, 2222, 2544};
String[] labels = {"1996", "1997", "1998", "1999", "2000", "2001", "2002", "2003", "2004", "2005"};

// Create a XYChart object of size 600 x 360 pixels
XYChart c = new XYChart(600, 360);

// Add a title to the chart using 18pt Times Bold Italic font
c.addTitle("Annual Revenue for Star Tech", "Times New Roman Bold Italic", 18);

// Set the plotarea at (60, 40) and of size 480 x 280 pixels. Use a vertical gradient color from
// light green (eeffee) to dark green (008800) as background. Set border and grid lines to white
// (ffffff).
c.setPlotArea(60, 40, 480, 280, c.linearGradientColor(60, 40, 60, 280, 0xeeffee, 0x008800), -1,
    0xffffff, 0xffffff);

// Add a multi-color bar chart layer using the revenue data.
BarLayer layer = c.addBarLayer3(revenue);

// Set cylinder bar shape
layer.setBarShape(Chart.CircleShape);

// Add extra field to the bars. These fields are used for showing additional information.
layer.addExtraField2(grossMargin);
layer.addExtraField2(backLog);

// Set the labels on the x axis.
c.xAxis().setLabels(labels);

// In this example, we show the same scale using both axes
c.syncYAxis();

// Set the axis line to transparent
c.xAxis().setColors(Chart.Transparent);
c.yAxis().setColors(Chart.Transparent);
c.yAxis2().setColors(Chart.Transparent);

// Set the axis label to using 8pt Arial Bold as font
c.yAxis().setLabelStyle("Arial Bold", 8);
c.yAxis2().setLabelStyle("Arial Bold", 8);
c.xAxis().setLabelStyle("Arial Bold", 8);

// Add title to the y axes
c.yAxis().setTitle("USD (millions)", "Arial Bold", 10);
c.yAxis2().setTitle("USD (millions)", "Arial Bold", 10);

// Output the chart
WebChartViewer viewer = new WebChartViewer(request, "chart1");
viewer.setChart(c, Chart.SVG);

// Client side Javascript to show detail information "onmouseover"
String showText = "onmouseover='showInfo(\"{xLabel}\", {value}, {field0}, {field1});' ";

// Client side Javascript to hide detail information "onmouseout"
String hideText = "onmouseout='showInfo(null);' ";

// "title" attribute to show tool tip
String toolTip = "title='{xLabel}: US$ {value|0}M'";

// Create an image map for the chart
String imageMap = c.getHTMLImageMap("xystub.jsp", "", showText + hideText + toolTip);

// Assign the image map to the chart
viewer.setImageMap(imageMap);
%&gt;
&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
    &lt;title&gt;Javascript Clickable Chart&lt;/title&gt;
    &lt;!-- Include ChartDirector Javascript Library to support chart interactions --&gt;
    &lt;script type="text/javascript" src="cdjcv.js"&gt;&lt;/script&gt;
&lt;/head&gt;
&lt;body style="margin:5px 0px 0px 5px"&gt;
&lt;script type="text/javascript"&gt;
//
//Client side script function to show and hide detail information.
//
function showInfo(year, revenue, grossMargin, backLog)
{
    var obj = document.getElementById('detailInfo');

    if (!year)
    {
        obj.style.visibility = "hidden";
        return;
    }

    var content = "&lt;table border='1' cellpadding='3' style='font:10pt verdana; " +
        "background-color:#CCCCFF' width='480px'&gt;";
    content += "&lt;tr&gt;&lt;td&gt;&lt;b&gt;Year&lt;/b&gt;&lt;/td&gt;&lt;td width='300px'&gt;" + year + "&lt;/td&gt;&lt;/tr&gt;";
    content += "&lt;tr&gt;&lt;td&gt;&lt;b&gt;Revenue&lt;/b&gt;&lt;/td&gt;&lt;td&gt;US$ " + revenue + "M&lt;/td&gt;&lt;/tr&gt;";
    content += "&lt;tr&gt;&lt;td&gt;&lt;b&gt;Gross Margin&lt;/b&gt;&lt;/td&gt;&lt;td&gt;" + grossMargin + "%&lt;/td&gt;&lt;/tr&gt;";
    content += "&lt;tr&gt;&lt;td&gt;&lt;b&gt;Back Log&lt;/b&gt;&lt;/td&gt;&lt;td&gt;US$ " + backLog + "M&lt;/td&gt;&lt;/tr&gt;";
    content += "&lt;/table&gt;";

    obj.innerHTML = content;
    obj.style.visibility = "visible";
}
&lt;/script&gt;
&lt;div style="font:bold 18pt verdana;"&gt;
    Javascript Clickable Chart
&lt;/div&gt;
&lt;hr style="border:solid 1px #000080" /&gt;
&lt;div style="font:10pt verdana; margin-bottom:20px"&gt;
    &lt;a href='viewsource.jsp?file=&lt;%=request.getServletPath()%&gt;'&gt;View Source Code&lt;/a&gt;
&lt;/div&gt;

&lt;!-- ****** Here is the chart image ****** --&gt;
&lt;%=viewer.renderHTML(response)%&gt;

&lt;div id="detailInfo" style="margin-left:60px"&gt;&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;</code></div><br>
The above code is quite similar to most of the sample codes in this documentation, so it will not be discussed in detail. The main difference is the code for creating the image map.<br><br>
<div class="codeblock"><code class='pre'>//Client side Javascript to show detail information "onmouseover"
String showText = "onmouseover='showInfo(\"{xLabel}\", {value}, {field0}, {field1});' ";

//Client side Javascript to hide detail information "onmouseout"
String hideText = "onmouseout='showInfo(null);' ";

//"title" attribute to show tool tip
String toolTip = "title='{xLabel}: USD {value|0}M'";

//Create an image map for the chart
String imageMap = c.getHTMLImageMap("xystub.jsp", "", showText + hideText + toolTip);</code></div><br>
Note that in the third argument of getHTMLImageMap, in addition to the "title" attribute for tooltips, it also includes "onmouseover" and "onmouseout" event handlers. They call the Javascript function "showInfo", which creates and hides the dynamic HTML block for displaying additional information.<br><br>
<div class="codeblock"><code class='pre'>&lt;script type="text/javascript"&gt;
//
//Client side script function to show and hide detail information.
//
function showInfo(year, revenue, grossMargin, backLog)
{
    var obj = document.getElementById('detailInfo');

    if (!year)
    {
        obj.style.visibility = "hidden";
        return;
    }

    var content = "&lt;table border='1' cellpadding='3' style='font:10pt verdana; " +
        "background-color:#CCCCFF' width='480px'&gt;";
    content += "&lt;tr&gt;&lt;td&gt;&lt;b&gt;Year&lt;/b&gt;&lt;/td&gt;&lt;td width='300px'&gt;" + year + "&lt;/td&gt;&lt;/tr&gt;";
    content += "&lt;tr&gt;&lt;td&gt;&lt;b&gt;Revenue&lt;/b&gt;&lt;/td&gt;&lt;td&gt;US$ " + revenue + "M&lt;/td&gt;&lt;/tr&gt;";
    content += "&lt;tr&gt;&lt;td&gt;&lt;b&gt;Gross Margin&lt;/b&gt;&lt;/td&gt;&lt;td&gt;" + grossMargin + "%&lt;/td&gt;&lt;/tr&gt;";
    content += "&lt;tr&gt;&lt;td&gt;&lt;b&gt;Back Log&lt;/b&gt;&lt;/td&gt;&lt;td&gt;US$ " + backLog + "M&lt;/td&gt;&lt;/tr&gt;";
    content += "&lt;/table&gt;";

    obj.innerHTML = content;
    obj.style.visibility = "visible";
}
&lt;/script&gt;</code></div><br>
In this example, we are displaying extra information, such as the "grossMargin" and "backLog" fields, which do not come from the x or y value of the bars. This is achieved by associated these fields as "extra fields" to the data items with <a href="Layer.addExtraField2.htm">Layer.addExtraField2</a>. These extra fields can then be represented using the template "{field<font color="#008800"><b><i>N</i></b></font>}", where "<font color="#008800"><b><i>N</i></b></font>" represents the field number. (The first field is 0, the second field is 1, etc.)<br><br>
In addition to responding to mouse over and mouse out events, the chart is also clickable using "xystub.jsp"  as the handler. For demo purpose, the handler simple displays information on what is clicked. It's code is as follows.<br><br>
<b>[JSP Version]</b> jspdemo/xystub.jsp
<div class='codeblock'><code class='pre'>&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
    &lt;title&gt;Simple Clickable XY Chart Handler&lt;/title&gt;
&lt;/head&gt;
&lt;body style="margin:5px 0px 0px 5px"&gt;
&lt;div style="font:bold 18pt verdana;"&gt;
    Simple Clickable XY Chart Handler
&lt;/div&gt;
&lt;hr style="border:solid 1px #000080" /&gt;
&lt;div style="font:10pt verdana; margin-bottom:20px"&gt;
    &lt;a href="viewsource.jsp?file=&lt;%=request.getServletPath()%&gt;"&gt;View Source Code&lt;/a&gt;
&lt;/div&gt;
&lt;div style="font:10pt verdana;"&gt;
&lt;b&gt;You have clicked on the following chart element :&lt;/b&gt;&lt;br /&gt;
&lt;ul&gt;
    &lt;li&gt;Data Set : &lt;%=request.getParameter("dataSetName")%&gt;&lt;/li&gt;
    &lt;li&gt;X Position : &lt;%=request.getParameter("x")%&gt;&lt;/li&gt;
    &lt;li&gt;X Label : &lt;%=request.getParameter("xLabel")%&gt;&lt;/li&gt;
    &lt;li&gt;Data Value : &lt;%=request.getParameter("value")%&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;</code></div></div><br>
<hr class="separator"><div class="copyright">&copy; 2022 Advanced Software Engineering Limited. All rights reserved.</div></body>
</HTML>
